//
//  PowerListController.swift
//  SCA
//
//  Created by tjun on 2025/9/7.
//

import UIKit

class PowerListController: BaseController {

    private var datas: [PowerListData] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = UITableView.automaticDimension
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(PowerListCell.self, forCellReuseIdentifier: PowerListCell.identifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        
        let headerView = UIView(frame: CGRectMake(0, 0, view.width, 40))
        let stateLabel = UILabel(text: "现在".localizedString, font: UIFontMediumMake(13), textColor: .white, textAlignment: .center).addTo(headerView)
        stateLabel.sizeToFit()
        stateLabel.addCorner(radius: 12)
        stateLabel.backgroundColor = .black
        stateLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(stateLabel.width + 50)
            make.height.equalTo(24)
        }
        
        tableView.tableHeaderView = headerView
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: NotificationName.buyPowerSucceed, object: nil)
        
        loadData()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc private func loadData() {
        ComputingPowerService.loadPoolData(success: { models in
            self.datas.removeAll()
            for model in models {
                self.datas.append(PowerListData(model: model))
            }
            self.tableView.reloadData()
        }) { error in
            
        }
    }
    
    override var hiddenNavigationBar: Bool {
        return true
    }

}

extension PowerListController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PowerListCell.identifier, for: indexPath) as! PowerListCell
        let data = datas[indexPath.row]
        cell.data = data
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = datas[indexPath.row].model
        let powerDetailController = PowerDetailController(pool: model.pool, poolName: model.title, poolIconUrl: model.icon)
        navigationController?.pushViewController(powerDetailController, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let data = datas[indexPath.row]
        return data.height
    }

}


